package com.zj.study.alibaba;

import com.aliyun.tair.tairhash.TairHash;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author 赵赳
 * @since 2022/8/2 9:29
 */
public class TairHashTest {

  // init timeout
  private static final int DEFAULT_CONNECTION_TIMEOUT = 5000;
  // api timeout
  private static final int DEFAULT_SO_TIMEOUT = 2000;
  private static final String HOST = "zhaojiu.top";
  private static final int PORT = 6379;
  private static final String PASSWORD = null;
  private static JedisPool jedisPool = null;
  private static final JedisPoolConfig config = new JedisPoolConfig();

  static {
    // 参数设置最佳实践可参考：https://help.aliyun.com/document_detail/98726.html
    config.setMaxTotal(32);
    config.setMaxIdle(32);
    config.setMaxIdle(20);

    jedisPool = new JedisPool(config, HOST, PORT, DEFAULT_CONNECTION_TIMEOUT,
        DEFAULT_SO_TIMEOUT, PASSWORD, 1, null);
  }


  public static long exhset(String key, String field, String value) {
    try {
      try (Jedis jedis = jedisPool.getResource()) {
        TairHash hash = new TairHash(jedis);
        return hash.exhset(key, field, value);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return 0;
  }

  public static String exhget(String key, String field) {
    try (Jedis jedis = jedisPool.getResource()) {
      TairHash hash = new TairHash(jedis);
      return hash.exhget(key, field);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }


  public static void main(String[] args) {
    String key = "real_time_data";
    String field = "item";
    String value = "value";

    System.out.println(exhset(key, field, value));
    System.out.println(exhget(key, field));

  }


}
